package com.xmkj.bearuser.home;

import android.support.v7.widget.LinearLayoutManager;

import com.common.adapter.helper.IRecyclerViewHelper;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.bean.TopFourBean;
import com.common.retrofit.bearusermethod.GetTopFourMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.bearuser.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by leo
 * 创建日期：2018/7/11
 * 描述：
 */

public class HomeTopFourActivity extends BaseMvpActivity {
    public static final String TYPE = "type";
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerview;
    private String type;
    List<TopFourBean> homeReccommedBeans = new ArrayList<>();
    private TopFourAdapter topFourAdapter;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_base_reclerview;
    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {
        setRecyclerView();
        gotoHttp();
    }

    private void gotoHttp() {
        CommonSubscriber<ArrayList<TopFourBean>> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                ArrayList<TopFourBean> beans = (ArrayList<TopFourBean>) o;
                List<TopFourBean> beanList = beans;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    topFourAdapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    homeReccommedBeans = beanList;
                    topFourAdapter.addAll(homeReccommedBeans);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                topFourAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                topFourAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        GetTopFourMethods.getInstance().activityList(subscriber, mPageIndex,type);
        rxManager.add(subscriber);
    }

    private void setRecyclerView() {
        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);

//        recyclerview.addItemDecoration(new DividerGridItemDecoration(context));
      /*  recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.HORIZONTAL, (int) SizeUtils.dp2px(context, 1)));*/
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(true);
        recyclerview.setLoadMoreEnabled(true);
        topFourAdapter = new TopFourAdapter(context, homeReccommedBeans);
//        adapter.setOnItemClickSeclect(this);
        recyclerview.setAdapter(topFourAdapter);

        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoHttp();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoHttp();
            }
        });

    }

    @Override
    protected void setNavigation() {
        type = getIntent().getStringExtra(TYPE);
        switch (type) {
            case "1":
                setNavigationBack("店铺活动");
                break;
            case "2":
                setNavigationBack("节日活动");
                break;
            case "3":
                setNavigationBack("会员专区");
                break;
            case "4":
                setNavigationBack("时尚资讯");
                break;
        }

    }


    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }
}
